package com.qcby.controller;


import cn.hutool.http.HttpResponse;
import com.qcby.entity.Clazz;
import com.qcby.entity.Score;
import com.qcby.entity.Student;
import com.qcby.service.Iface.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/student")
public class studentController {

    @Resource
    private StudentService studentService;
    @RequestMapping("/homePage.do")
    public String returnHomePage(){
        return "indexStudent";
    }
    @RequestMapping("/findAll.do")
    @ResponseBody
    public  List<Student> findAll(){
        return studentService.findAll();
    }
    @RequestMapping("/findAllCount.do")
    @ResponseBody
    public int findAllCount(){
        return studentService.findAllCount();
    }
    @RequestMapping("/findAllCountBySearch.do")
    @ResponseBody
    public int findAllCountBySearch(HttpServletRequest request){
        String name= "%"+request.getParameter("name")+"%";
        String sex=  "%"+request.getParameter("sex")+"%";
        Student student=new Student();
        student.setSex(sex);
        student.setName(name);
        return studentService.findAllCountBySearch(student);
    }
    @RequestMapping("/findAllBySearch.do")
    @ResponseBody
    public List<Student> findAllBySearch(HttpServletRequest request){
        Student student=new Student();
        student.setSex("%"+request.getParameter("sex")+"%");
        student.setName("%"+request.getParameter("name")+"%");
        student.setStartPage(request.getParameter("startPage"));
        student.setEndPage(request.getParameter("endPage"));
        System.out.println(student);
        return studentService.findAllBySearch(student);
    }
    @RequestMapping("/addPage.do")
    public String hrefAddPage(){
        return "addStudent";
    }
    @RequestMapping("backHomePage.do")
    public String backToHomePage(){
        return "indexStudent";
    }
    @RequestMapping("/addStudent.do")
    @ResponseBody
    public int addStudent(Student student){
        return studentService.addStudent(student);
    }
    @RequestMapping("/delete.do")
    @ResponseBody
    public int deleteStudent(HttpServletRequest httpServletRequest){
        int id= Integer.parseInt(httpServletRequest.getParameter("id"));
        return studentService.deleteStudent(id);
    }
    @RequestMapping("/update.do")
    @ResponseBody
    public int  updateStudent(Student student){
        return studentService.updateStudent(student);
    }
    @RequestMapping("/updatePage.do")
    public String returnUpdatePage(){
        return "updateStudent";
    }
    @RequestMapping("/updateStudentInfro.do")
    @ResponseBody
    public Student updateStudentInfro(HttpServletRequest httpServletRequest){
        int id= Integer.parseInt(httpServletRequest.getParameter("id"));
        return  studentService.updateStudentInfro(id);
    }
    @RequestMapping("/loadClass.do")
    @ResponseBody
    public List<Clazz> loadClass(){
        return studentService.loadClass();
    }
    @RequestMapping("/lookOther")
    public String lookOther(){
        return "detailStudent";
    }
    @RequestMapping("/findClassById.do")
    @ResponseBody
    public Clazz findClassById(HttpServletRequest request){
        String id=request.getParameter("id");
        return studentService.findClassById(id);
    }
    @RequestMapping("/oneStudentScore.do")
    @ResponseBody
    public List<Score> oneStudentScore(HttpServletRequest request){
        String id=request.getParameter("id");
        System.out.println(studentService.oneStudentScore(id));
        return studentService.oneStudentScore(id);
    }
    @RequestMapping("/toOneScoreData.do")
    public String ToOneScoreData(){
        return "oneStudentScore";
    }
    @RequestMapping("/returnOneStudentInfro.do")
    public String returnOneStudentInfro(){
        return "detailStudent";
    }
}
